package com.wgh.product.service.impl;

import com.wgh.product.entity.shop.ProductRelation;
import com.wgh.product.mapper.ProductRelationMapper;
import com.wgh.product.service.ProductRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品关联Service实现类
 * 
 * @author AI Shop
 */
@Service
public class ProductRelationServiceImpl extends ServiceImpl<ProductRelationMapper, ProductRelation> implements ProductRelationService {

    @Override
    public List<ProductRelation> getRelationsByMainProductIdAndType(Long mainProductId, Integer relationType) {
        return baseMapper.selectByMainProductIdAndType(mainProductId, relationType);
    }

    @Override
    public List<ProductRelation> getRelationsByRelatedProductIdAndType(Long relatedProductId, Integer relationType) {
        return baseMapper.selectByRelatedProductIdAndType(relatedProductId, relationType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addBatch(List<ProductRelation> productRelations) {
        return baseMapper.insertBatch(productRelations) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteByMainProductId(Long mainProductId) {
        return baseMapper.deleteByMainProductId(mainProductId) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteByMainProductIdAndType(Long mainProductId, Integer relationType) {
        return baseMapper.deleteByMainProductIdAndType(mainProductId, relationType) > 0;
    }

    @Override
    public boolean checkRelationExists(Long mainProductId, Long relatedProductId, Integer relationType) {
        return baseMapper.countRelation(mainProductId, relatedProductId, relationType) > 0;
    }
}